// Sprite.cpp: implementation of the Sprite class.
//
//////////////////////////////////////////////////////////////////////

#include "Sprite.h"
#include <stdio.h>
#include <GL/glaux.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSprite::CSprite(int x, int y) : PosX(x), PosY(y)
{
}

CSprite::~CSprite()
{

}

////////////////////////////////////////////////////////////////////////////////////////
// Load a Bitmap file
////////////////////////////////////////////////////////////////////////////////////////
AUX_RGBImageRec* CSprite::Loadbmp(char *Filename)				// Loads A Bitmap Image
{
	FILE *File=NULL;									// File Handle

	if (!Filename)										// Make Sure A Filename Was Given
	{
		return NULL;									// If Not Return NULL
	}

	File=fopen(Filename,"r");							// Check To See If The File Exists

	if (File)											// Does The File Exist?
	{
		fclose(File);									// Close The Handle
		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
	}

	return NULL;										// If Load Failed Return NULL
}


int CSprite::LoadGLTextures(char *Filename, int numtexture)              		// Load Bitmaps And Convert To Textures
{
	int Status=FALSE;									// Status Indicator

	AUX_RGBImageRec* TextureImage=NULL;		// Create Storage Space For The Texture		
	//memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL
	//memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL

	texture = new GLuint[2];
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
	glGenTextures(2, &texture[0] );					// Create numtexures Textures


	// mask
	    char temp[50] = "mask";
	    strcat(temp,Filename);

		TextureImage = Loadbmp(temp);		

		SpriteWidth = TextureImage->sizeX;
		SpriteHeight = TextureImage->sizeY;

		// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
		Status=TRUE;									// Set The Status To TRUE		

		// Create Nearest Filtered Texture
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);

	// sprite
		TextureImage = Loadbmp(Filename);		

		SpriteWidth = TextureImage->sizeX;
		SpriteHeight = TextureImage->sizeY;

		// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
		Status=TRUE;									// Set The Status To TRUE		

		// Create Nearest Filtered Texture
		glBindTexture(GL_TEXTURE_2D, texture[1]);
		//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);


	if (TextureImage)
	{
		if (TextureImage->data)
		{
			free(TextureImage->data);
		}
	    free(TextureImage);
	}	

	return Status;										// Return The Status
}

void CSprite::DrawSprite(int xPos, int yPos, int SpriteID )
{
	PosX = xPos;
	PosY = yPos;

	glEnable(GL_BLEND);							// Enable Blending	
	glDisable(GL_DEPTH_TEST);						
	
    glBlendFunc(GL_DST_COLOR,GL_ZERO);				

	// tile now holds the ID# of the tile at location map[y][x].
	glBindTexture(GL_TEXTURE_2D, texture[SpriteID]);
							
	// Here we bind the texture that we need, either texture[0] or texture[1], based on the value of tile, which holds the current tile's ID#.	
    glBegin(GL_QUADS);
	    glTexCoord2f(0.0f, 0.0f); glVertex3f(float(PosX), float(PosY), 0.0f);
	    glTexCoord2f(1.0f, 0.0f); glVertex3f(float(PosX + SpriteWidth), float(PosY), 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(float(PosX + SpriteWidth), float(PosY + SpriteHeight), 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(float(PosX), float(PosY + SpriteHeight), 0.0f);
	glEnd();

	glBlendFunc(GL_ONE, GL_ONE);					

	glBindTexture(GL_TEXTURE_2D, texture[SpriteID+1]);
    glBegin(GL_QUADS);
	    glTexCoord2f(0.0f, 0.0f); glVertex3f(float(PosX), float(PosY), 0.0f);
	    glTexCoord2f(1.0f, 0.0f); glVertex3f(float(PosX + SpriteWidth), float(PosY), 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(float(PosX + SpriteWidth), float(PosY + SpriteHeight), 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(float(PosX), float(PosY + SpriteHeight), 0.0f);
	glEnd();

	glEnable(GL_DEPTH_TEST);						// Enable Depth Testing
	glDisable(GL_BLEND);							
}

void CSprite::DrawSprite(int SpriteID )
{
	glEnable(GL_BLEND);							// Enable Blending	
	glDisable(GL_DEPTH_TEST);						
	
    glBlendFunc(GL_DST_COLOR,GL_ZERO);				

	// tile now holds the ID# of the tile at location map[y][x].
	glBindTexture(GL_TEXTURE_2D, texture[SpriteID]);
							
	// Here we bind the texture that we need, either texture[0] or texture[1], based on the value of tile, which holds the current tile's ID#.	
    glBegin(GL_QUADS);
	    glTexCoord2f(0.0f, 0.0f); glVertex3f(float(PosX), float(PosY), 0.0f);
	    glTexCoord2f(1.0f, 0.0f); glVertex3f(float(PosX + SpriteWidth), float(PosY), 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(float(PosX + SpriteWidth), float(PosY + SpriteHeight), 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(float(PosX), float(PosY + SpriteHeight), 0.0f);
	glEnd();

	glBlendFunc(GL_ONE, GL_ONE);					

	glBindTexture(GL_TEXTURE_2D, texture[SpriteID+1]);
    glBegin(GL_QUADS);
	    glTexCoord2f(0.0f, 0.0f); glVertex3f(float(PosX), float(PosY), 0.0f);
	    glTexCoord2f(1.0f, 0.0f); glVertex3f(float(PosX + SpriteWidth), float(PosY), 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(float(PosX + SpriteWidth), float(PosY + SpriteHeight), 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(float(PosX), float(PosY + SpriteHeight), 0.0f);
	glEnd();

	glEnable(GL_DEPTH_TEST);						// Enable Depth Testing
	glDisable(GL_BLEND);							

}

void CSprite::MoveRight(int npixel)
{
	PosX+=npixel;
}

void CSprite::MoveLeft(int npixel)
{
	PosX-=npixel;
}

void CSprite::MoveDown(int npixel)
{
	PosY-=npixel;
}

void CSprite::MoveUp(int npixel)
{
	PosY+=npixel;
}
